#include "record.h"
#include "ui_record.h"
#include "GameUI/recorditem.h"
#include "ResourceManger/filemanager.h"
Record::Record(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Record)
{
    Init();
}

Record::~Record()
{
    delete ui;
}

void Record::AddRecord(RecordItem *ri)
{
    QListWidgetItem* item = new QListWidgetItem();
    item->setSizeHint(QSize(600,600));

    if(!ri)
    {
        ri = new RecordItem();
        ri->empty = true;
        ri->id = 0;
        int count = ui->RecordList->count();
        if(count > 0)
        {
            RecordItem* item = dynamic_cast<RecordItem*>(
                        ui->RecordList->itemWidget(ui->RecordList->item(count - 1)));
            if(item)
                ri->id = item->id + 1;
        }
    }
    else
    {
        ri->Refresh(ri->narrationId);
    }
    ui->RecordList->addItem(item);
    ui->RecordList->setItemWidget(item,ri);
    Save();
}

void Record::RemoveRecord(int id)
{
    for(int i = 0;i < ui->RecordList->count(); i++)
    {
        QListWidgetItem* item = ui->RecordList->item(i);
        if(!item)
            continue;
        RecordItem* ri = dynamic_cast<RecordItem*>(ui->RecordList->itemWidget(item));
        if(!ri)
            continue;
        if(ri->id == id)
        {
            ui->RecordList->takeItem(i);
            delete item;
            delete ri;
            item = nullptr;
            ri = nullptr;
            //删除json文件中的该项

            QString path = FileManager::Instance().recordPath;
            QJsonObject* ob = FileManager::Instance().ReadJsonFile(path);
            ob->remove(QString::number(id));
            JsonTranslator jt;
            FileManager::Instance().Write(path,jt.Json2Byte(ob));

            return;
        }
    }
}

void Record::showEvent(QShowEvent *event)
{
    QWidget::showEvent(event);
    for(int i = 0;i < ui->RecordList->count();i++)
    {
        QListWidgetItem* item = ui->RecordList->item(i);
        if(!item)
            continue;
        RecordItem* ri = dynamic_cast<RecordItem*>(ui->RecordList->itemWidget(item));
        if(!ri)
            continue;
        ri->Switch(false);
    }
}

void Record::Init()
{
    ui->setupUi(this);
    //从左到右显示
    ui->RecordList->setFlow(QListView::LeftToRight);
    ui->RecordList->setSelectionMode(QAbstractItemView::NoSelection);
    QString qss0 = "QListWidget{border-image:url(\"";
    QString qss1 = "\");padding-left: 20px;padding-right: 20px;padding-top:30px;padding-bottom:30px;}";
    if(UILib::Instance().labels["存档-容器"])
    {
        ui->RecordList->setStyleSheet(qss0 + UILib::Instance().labels["存档-容器"]->img + qss1);
    }
    if(UILib::Instance().labels["存档-存档框"])
    {
        img = UILib::Instance().labels["存档-存档框"]->img;
    }
    connect(&EventCenter::Instance(),&EventCenter::UIChange,this,[=](QString name){
        if(name == "存档-容器" && UILib::Instance().labels[name])
        {
            ui->RecordList->setStyleSheet(qss0 + UILib::Instance().labels[name]->img + qss1);
        }
        if(name == "存档-存档框" && UILib::Instance().labels[name])
        {
            img = UILib::Instance().labels[name]->img;
        }
    });
    //删除
    connect(&EventCenter::Instance(),&EventCenter::DeleteRecord,this,[=](int id){
        RemoveRecord(id);
    });
    connect(&EventCenter::Instance(),&EventCenter::AddRecord,this,[=](){
        AddRecord();
    });
    if(UILib::Instance().btns.contains("返回主页") && UILib::Instance().btns["返回主页"])
        ui->HomeBtn->info = UILib::Instance().btns["返回主页"];
    ReadAllRecord();
}

void Record::ReadAllRecord()
{
    QString path = FileManager::Instance().recordPath;
    QJsonObject* ob = FileManager::Instance().ReadJsonFile(path);
    JsonTranslator jt;
    if(ob)
    {
        for(auto it = (*ob).begin();it != (*ob).end();it++)
        {
            QJsonObject riob = it.value().toObject();
            RecordItem* item = jt.RecordFromJson(&riob);
            AddRecord(item);
        }
    }
    //空栏位
    AddRecord();
}

void Record::Save()
{
    QJsonObject* ob = new QJsonObject();
    JsonTranslator jt;
    QString path = FileManager::Instance().recordPath;
    QVector<QJsonObject*> tmp;

    for(int i = 0;i < ui->RecordList->count() - 1; i++)
    {
        QListWidgetItem* item = ui->RecordList->item(i);
        if(!item)
            continue;
        RecordItem* ri = dynamic_cast<RecordItem*>(ui->RecordList->itemWidget(item));
        if(!ri)
            continue;
        QJsonObject* riob = jt.ToJson(ri);
        if(riob)
        {
            tmp.push_back(riob);
            ob->insert(QString::number(ri->id),*riob);
        }
    }
    FileManager::Instance().Write(path,jt.Json2Byte(ob));
    //释放内存
    for(int i = 0;i < tmp.count();i++)
    {
        if(tmp[i])
        {
            delete tmp[i];
            tmp[i] = nullptr;
        }
    }
    if(ob)
        delete ob;
    ob = nullptr;
}
